package auth

import (
	"goskeleton/app/global/consts"
	modeAuth "goskeleton/app/model/auth"
	"goskeleton/app/utils/response"

	"github.com/gin-gonic/gin"
)

type RoleController struct {
}

// Create 新增角色
func (r *RoleController) Create(c *gin.Context) {
	if modeAuth.CreateAuthOrganizationFactory("").InsertRoleData(c) {
		response.Success(c, consts.CurdStatusOkMsg, "角色创建成功")
	} else {
		response.Fail(c, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+"请注意不要添加重复的角色名称", "")
	}
}

// Edit 编辑角色
func (r *RoleController) Edit(c *gin.Context) {
	if modeAuth.CreateAuthOrganizationFactory("").UpdateRoleData(c) {
		response.Success(c, consts.CurdStatusOkMsg, "角色更新成功")
	} else {
		response.Fail(c, consts.CurdUpdateFailCode, consts.CurdUpdateFailMsg, "")
	}
}

// Detail 获取角色详情
func (r *RoleController) Detail(c *gin.Context) {
	id := c.GetFloat64(consts.ValidatorPrefix + "id")

	roleModel := modeAuth.CreateAuthOrganizationFactory("")
	data, err := roleModel.GetRoleById(int(id))

	if err != nil {
		response.Fail(c, consts.CurdSelectFailCode, "角色不存在或非角色类型数据", err.Error())
	} else {
		response.Success(c, consts.CurdStatusOkMsg, data)
	}
}
